package qf.dao.daoImpl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import qf.dao.GoodsDao;
import qf.pojo.Goods;
import qf.utils.DBUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 86198
 * @version V1.0
 * @Project Java_qf
 * @Package qf.dao.daoImpl
 * @Description:
 * @Date 2022/5/24 20:14
 */
public class GoodsDaoImpl implements GoodsDao {
    private QueryRunner qr = new QueryRunner(DBUtil.getDateSource());
    @Override
    public List<Goods> showGoods() {
        String sql = "select a.*,b.name typeName from tb_goods a left join tb_goods_type b on a.id = b.id";
        try {
            List<Goods> query = qr.query(sql, new BeanListHandler<Goods>(Goods.class));
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void insertGoods(Goods goods) {
        String sql = "insert into tb_goods(name, pubdate, picture, price, star, intro, tid) value(?, ?, ? ,? ,?, ?, ?)";
        try {
            qr.update(sql, goods.getName(), goods.getPubdate(), goods.getPicture(),
                    goods.getPrice(), goods.getStar(), goods.getIntro(), goods.getTid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Goods selectGoodsBYID(Integer id) {
        String sql = "select * from tb_goods where id=?";
        try {
            return qr.query(sql,new BeanHandler<Goods>(Goods.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
